package cn.kduck.code.module.query;

import cn.kduck.core.dao.definition.BeanDefDepository;
import cn.kduck.core.dao.definition.BeanEntityDef;
import cn.kduck.core.dao.query.QueryCreator;
import cn.kduck.core.dao.query.QuerySupport;
import cn.kduck.core.dao.sqlbuilder.ConditionBuilder.ConditionType;
import cn.kduck.core.dao.sqlbuilder.SelectBuilder;
import org.springframework.stereotype.Component;

import java.util.Map;

import static cn.kduck.code.entity.service.EntityService.CODE_ENTITY;
import static cn.kduck.code.module.service.ModuleService.CODE_MODULE_ENTITY;

@Component
public class ModuleEntityQuery implements QueryCreator {
    @Override
    public QuerySupport createQuery(Map<String, Object> paramMap, BeanDefDepository depository) {
        BeanEntityDef moduleEntityDef = depository.getEntityDef(CODE_MODULE_ENTITY);
        BeanEntityDef entityDef = depository.getEntityDef(CODE_ENTITY);
        SelectBuilder selectBuilder = new SelectBuilder(paramMap);
        selectBuilder.bindFields("e",entityDef.getFieldList());
        selectBuilder.from("m",moduleEntityDef).innerJoinOn("e",entityDef,"entityId");
        selectBuilder.where().and("m.MODULE_ID", ConditionType.EQUALS, "moduleId",true);
        return selectBuilder.build();
    }
}
